package com.liukai.novel_type.fragment;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.google.android.material.tabs.TabLayout;
import com.liukai.novel_base.base.BaseFragment;
import com.liukai.novel_base.base.BasePresenter;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.NovelType;
import com.liukai.novel_commons.utils.JSONUtil;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.State;
import com.liukai.novel_rack.ui.SearchActivity;
import com.liukai.novel_type.R;
import com.liukai.novel_type.R2;
import com.liukai.novel_type.adapter.NovelTypePagerAdapter;
import com.liukai.novel_type.call_back.INovelTypeCallBack;
import com.liukai.novel_type.presenter.INovelTypePresenter;
import com.liukai.novel_type.presenter.impl.NovelTypePresenterImpl;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class NovelTypeFragment extends BaseFragment implements INovelTypeCallBack {

    @BindView(R2.id.novel_type_indicator)
    TabLayout mTabLayout;

    @BindView(R2.id.novel_type_pager)
    ViewPager mViewPager;

    private NovelTypePresenterImpl mNovelTypePresenter;
    private NovelTypePagerAdapter mNovelTypePagerAdapter;

    @BindView(R2.id.toolbar)
    View toolbar;
    private View mBtnMore;
    private View mBtnSearch;
    private Context mContext;

    /**
     * 初始化数据
     */
    @Override
    protected void initDate(View view) {
        super.initDate(view);
        setUpState(State.LOADING);
        mNovelTypePresenter = new NovelTypePresenterImpl();
        mNovelTypePresenter.registerCallback(this);
        mNovelTypePresenter.findAllNovelType();
    }


    /**
     * 获取Fragment的上下文
     *
     * @param view
     * @return
     */
    @Override
    protected Context getFragmentContext(View view) {
        mContext = view.getContext();
        return null;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mTabLayout.setupWithViewPager(mViewPager);
        // 给ViewPager设置适配器
        mNovelTypePagerAdapter = new NovelTypePagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mNovelTypePagerAdapter);

        mBtnSearch = toolbar.findViewById(R.id.layout_search);
        mBtnMore = toolbar.findViewById(R.id.layout_more);

    }

    /**
     * 绑定组件
     * @param view
     */


    /**
     * 设置适配器
     */
    @Override
    protected void initAdapter(View view) {

    }

    /**
     * 设置监听
     */
    @Override
    protected void initListener() {
        mBtnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "搜索", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(mContext, SearchActivity.class));
            }
        });

        mBtnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "更多", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取布局的ID
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_novel_type;
    }

    @Override
    protected void release() {
        super.release();
        mNovelTypePresenter.unRegisterCallback(this);
    }

    /**
     * 获取服务端返回的小说分类
     *
     * @param result
     */
    @Override
    public void resultFindAllNovelType(Result result) {

        // 网络请求失败
        if (result.getCode() == 404) {
            setUpState(State.ERROR);
            return;
        }
        // 解析result，提取小说分类列表
        List<NovelType> novelTypeList = JSONUtil.resolverResultList(result, NovelType.class);
        // 如果小说分类列表为空，页面显示位空页面
        if (novelTypeList.size() == 0 || novelTypeList == null) {
            setUpState(State.EMPTY);
        } else {
            // 页面显示为成功页面
            setUpState(State.SUCCESS);
        }
        if (mNovelTypePagerAdapter != null) {
            // 将小说分类列表传给适配器
            mNovelTypePagerAdapter.setNovelType(novelTypeList);
        }
    }
}
